<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 头部搜索区 -->
      <el-row>
        <el-col>
          <el-input
            placeholder="请输入内容"
            v-model="query.query"
            @change="getTabelList"
            clearable
            @clear="getTabelList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getTabelList"></el-button>
          </el-input>
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区 -->
      <el-table :data="tabelList" stripe style="width: 100%" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="180"></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">{{ scope.row.add_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查找参数
      //定义查询参数
      query: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 商品表格数据
      tabelList: [],
      //商品总数
      total: 0,
    };
  },
  created() {
    this.getTabelList();
  },
  methods: {
    //   获取商品数据
    async getTabelList() {
      const { data: res } = await this.axios.get("goods", {
        params: this.query,
      });
      if (res.meta.status !== 200) {
        return layer.msg(res.meta.msg, { time: 1000 });
      }
      this.total = res.data.total;
      this.tabelList = res.data.goods;
    },
    //页面数据大小发生改变
    handleSizeChange(newPageSize) {
      this.query.pagesize = newPageSize;
      this.getTabelList();
    },
    //页码发生改变
    handleCurrentChange(newPageNum) {
      this.query.pagenum = newPageNum;
      this.getTabelList();
    },
    //addGoods添加商品
    addGoods() {
      this.$router.push("/goods/add");
    },
    //点击编辑商品
    editGoods(row) {
      console.log(row);
    },
    //点击删除商品
    deleteGoods(row) {
      layer.confirm(
        "此操作将永久删除该分类, 是否继续?",
        { icon: 3, title: "提示" },
        async (index) => {
          const { data: res } = await this.axios.delete(
            "goods/" + row.goods_id
          );
          if (res.meta.status !== 200) {
            layer.msg(res.meta.msg, { time: 1000 });
          }
          layer.msg(res.meta.msg, { time: 1000 }, () => {
            this.getTabelList();
            layer.close(index);
          });
        }
      );
    },
  },
};
</script>

<style scoped>
.el-input {
  width: 500px;
}
.el-button {
  margin-left: 20px;
}
</style>